package com.zisuyi.game.sdk.library.login

import android.app.Activity
import android.app.Application
import android.content.Intent
import androidx.annotation.Keep
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.LifecycleOwner
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
import com.google.gson.annotations.SerializedName
import com.weilele.mvvm.app
import com.weilele.mvvm.base.helper.ILifecycleObserver
import com.zisuyi.game.sdk.library.R
import com.zisuyi.game.sdk.library.login.impl.GameLoginFacebookImpl
import com.zisuyi.game.sdk.library.login.impl.GameLoginGoogleImpl
import com.zisuyi.game.sdk.library.login.impl.GameLoginOneTapImpl

interface IGameLogin : ILifecycleObserver {
    companion object {
        const val LOGIN_FACEBOOK = "facebook"
        const val LOGIN_GOOGLE = "google"
        const val LOGIN_HUAWEO = "huawei"

        operator fun invoke(
                manager: FragmentManager,
                lifecycleOwner: LifecycleOwner,
                @LoginType type: String
        ): IGameLogin {
            return when (type) {
                LOGIN_FACEBOOK -> {
                    GameLoginFacebookImpl(manager, lifecycleOwner)
                }
                LOGIN_GOOGLE -> {
                    GameLoginOneTapImpl(manager, lifecycleOwner)
//                    GameLoginGoogleImpl(manager, lifecycleOwner)
                }
                else -> {
                    GameLoginFacebookImpl(manager, lifecycleOwner)
                }
            }
        }

        fun init(application: Application) {
            GameLoginFacebookImpl.init(application)
        }

        /**
         * 登出
         */
        fun logOut(activity: Activity) {
            GameLoginFacebookImpl.logout()
            GameLoginGoogleImpl.logout()
            GameLoginOneTapImpl.logout(activity)
        }

        /**
         * 获取上一次的登录
         * 没有返回null
         */
        fun lastUserInfo(): UserInfo? {
            return GameLoginFacebookImpl.lastLoginUserInfo()
                    ?: GameLoginGoogleImpl.lastLoginUserInfo()
        }
    }

    @Keep
    data class UserInfo(
            @SerializedName("id") val id: String,
            @SerializedName("token") val token: String,
            @SerializedName("type") val type: String,
            @SerializedName("name") val name: String?,
            @SerializedName("thumb") val thumb: String?,
    )

    interface OnLoginListener {
        fun onSuccess(info: UserInfo, @LoginType type: String)
        fun onCancel(@LoginType type: String)
        fun onError(error: Throwable, @LoginType type: String)
    }

    /**
     * 请求登录
     */
    fun requestLogin()

    /**
     * 登出
     */
    fun logOut()

    /**
     * 添加登录监听
     */
    fun addOnLoginListener(listener: OnLoginListener)

    /**
     * 移除登录监听
     */
    fun removeOnLoginListener(listener: OnLoginListener)
}